<?php

/**
 * documentos_beca actions.
 *
 * @package    auip
 * @subpackage documentos_beca
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class documentos_becaActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    //$this->documentos_becass = Doctrine_Core::getTable('DocumentosBecas')->createQuery('a')->execute();
      $con = Doctrine_Manager::getInstance()->connection();
      $query = "SELECT db.beca_id, db.documento_id, b.nombre as nombre_beca, d.nombre as nombre_tipo FROM (documentos_becas as db INNER JOIN beca as b ON db.beca_id= b.id) INNER JOIN tipo_documento as d ON db.documento_id = d.id ORDER BY b.id;";      
      $st = $con->execute($query);
      $nombres = $st->fetchAll();
      
      
      $id_becas = array();
      $id_docs = array();
      $tipos = array();
      $total = array();
      $r=array();
           
      foreach ($nombres as $nom):          
          $id_becas[] = $nom[0];          
      endforeach;
            
      $result = array_unique($id_becas);  
      
      $result = array_values($result);
      
      
//      for($i=0; $i <count($result); $i++)
//      {     
//            $r[] =  $nombres[$i]['beca_id'];
//            $r[] =  $nombres[$i]['nombre_beca'];            
//            for($j=0; $j <count($nombres); $j++)
//            {
//                
//                $n = $nombres[$j];
//                unset($n[0]);
//                unset($n[1]);
//                unset($n[2]);            
//                unset($n[3]);              
//                print $result[$i].'=='.$n['beca_id'];
//                print '<br>';
//              if ($result[$i] == $n['beca_id']) 
//              {                
//                $id_docs[]= $n['documento_id'];                                
//                $tipos[]= $n['nombre_tipo'];                 
//              }              
//            }
//            
//            $r[] = $id_docs;
//            $r[] = $tipos; 
////            print '<br>';
////            print_r($r);
////            print '<br>';
//            $total[] = $r;           
//            $r = array();
//            
//            $id_docs = array();
//            $tipos = array();
//            
//                         
//                    unset($nombres['beca_id']);
//                    $nombres = array_values($nombres);
//                    
//              
//            
//                print '<br>';
//                print_r($nombres[$i]);
//                print '<br>';                
//      }
      
      
      
      
      foreach ($nombres as $n)
      {     
                unset($n[0]);
                unset($n[1]);
                unset($n[2]);            
                unset($n[3]);
            $r[] =  $n['beca_id'];
            $r[] =  $n['nombre_beca'];            
                            
                
                for($i=0; $i < count($result); $i++)
                {
                    if ($result[$i] == $n['beca_id']) 
                    {                
                        $id_docs[]= $n['documento_id'];                                
                        $tipos[]= $n['nombre_tipo'];                 
                    }              
                }
            
            $r[] = $id_docs;
            $r[] = $tipos;             
            $total[] = $r;           
            
            $r = array();
            $id_docs = array();
            $tipos = array();
      }
             
      
//      foreach ($total as $t):
//          print '<br>';
//            print_r($t);
//         print '<br>';
//      endforeach;
      $this->codigos = $total;      
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->documentos_becas = Doctrine_Core::getTable('DocumentosBecas')->find(array($request->getParameter('beca_id'),
                                  $request->getParameter('documento_id')));
    $this->forward404Unless($this->documentos_becas);
  }

  public function executeNew(sfWebRequest $request)
  {
    //$this->form = new DocumentosBecasForm();
      
      $con = Doctrine_Manager::getInstance()->connection();
      $query = "SELECT id, nombre FROM beca ORDER BY(nombre);;";
      $st = $con->execute($query);
      $this->convocatorias = $st->fetchAll();
      
      $query1 = "SELECT id, nombre FROM tipo_documento ORDER BY(nombre);";
      $st1 = $con->execute($query1);
      $this->documentos = $st1->fetchAll();
  }
/************************************************************************************************/
   
  public function executeProcesador(sfWebRequest $request)
  {      
      $con = Doctrine_Manager::getInstance()->connection();      
      $query = "SELECT id, nombre FROM tipo_documento ORDER BY(id);";
      $st = $con->execute($query);
      $entidades = $st->fetchAll();

      $opciones = array();
      
                foreach ($entidades as $entidad) 
                {
                    $opciones[]=$entidad[0];
                    $opciones[]=$entidad[1];
                }
        echo json_encode($opciones);
        //print_r($entidades);
  }    
  public function executeContinuar(sfWebRequest $request)
  {
      $valor1 = $request->getParameter('convocatoria');
      $valor2 = $request->getParameter('tipos');                  
      $this->Guardarentidad($valor1, $valor2);
      $this->redirect('documentos_beca/index');
  }
  protected function Guardarentidad($id_beca, $tipos)  
  {
         $con = Doctrine_Manager::getInstance()->connection();
         for ($i=0; $i<count($tipos); $i++)    
         {             
            $query= "INSERT INTO documentos_becas(beca_id, documento_id) VALUES (". $id_beca .", ". $tipos[$i].");";              
            $st = $con->execute($query);
         }                  
  }  
  /****************************************************************************************************/
  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new DocumentosBecasForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($documentos_becas = Doctrine_Core::getTable('DocumentosBecas')->find(array($request->getParameter('beca_id'),
            $request->getParameter('documento_id'))), sprintf('Object documentos_becas does not exist (%s).', $request->getParameter('beca_id'),
            $request->getParameter('documento_id')));
    $this->form = new DocumentosBecasForm($documentos_becas);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($documentos_becas = Doctrine_Core::getTable('DocumentosBecas')->find(array($request->getParameter('beca_id'),
            $request->getParameter('documento_id'))), sprintf('Object documentos_becas does not exist (%s).', $request->getParameter('beca_id'),
            $request->getParameter('documento_id')));
    $this->form = new DocumentosBecasForm($documentos_becas);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($documentos_becas = Doctrine_Core::getTable('DocumentosBecas')->find(array($request->getParameter('beca_id'),
            $request->getParameter('documento_id'))), sprintf('Object documentos_becas does not exist (%s).', $request->getParameter('beca_id'),
            $request->getParameter('documento_id')));
    $documentos_becas->delete();

    $this->redirect('documentos_beca/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $documentos_becas = $form->save();

      $this->redirect('documentos_beca/edit?beca_id='.$documentos_becas->getBecaId().'&documento_id='.$documentos_becas->getDocumentoId());
    }
  }
}
